import tkinter as tk
from tkinter import ttk, filedialog

class ToolTip:
    def __init__(self, widgets, text):
        if not isinstance(widgets, (list, tuple)):
            widgets = [widgets]
        self.widgets = widgets
        self.text = text
        self.tip_window = None
        for widget in self.widgets:
            widget.bind("<Enter>", self.show_tip)
            widget.bind("<Leave>", self.hide_tip)

    def show_tip(self, event=None):
        if self.tip_window or not self.text:
            return
        x, y, _, _ = self.widgets[0].bbox("insert")
        x += self.widgets[0].winfo_rootx() + 25
        y += self.widgets[0].winfo_rooty() + 25
        self.tip_window = tw = tk.Toplevel(self.widgets[0])
        tw.wm_overrideredirect(True)
        tw.wm_geometry(f"+{x}+{y}")
        label = tk.Label(tw, text=self.text, justify=tk.LEFT,
                        background="#ffffe0", relief=tk.SOLID, borderwidth=1)
        label.pack()

    def hide_tip(self, event=None):
        if self.tip_window:
            self.tip_window.destroy()
        self.tip_window = None

class FileSettingsPanel(ttk.LabelFrame):
    """纯文件选择面板"""
    
    def __init__(self, parent, load_points_callback, load_matrix_callback):
        super().__init__(parent, text="文件设置")
        self.load_points_callback = load_points_callback
        self.load_matrix_callback = load_matrix_callback
        self.create_widgets()

    def create_widgets(self):
        """创建文件选择组件"""
        
        # Store references to widgets that need to be disabled
        self.widgets_to_disable = []
        
        # 创建文件组容器
        file_group = ttk.Frame(self)
        file_group.pack(fill=tk.X, pady=5)
        
        self.btn_points = ttk.Button(file_group, text="选择点位与积分信息EXCEL文件",
                 command=self.load_points_callback)
        self.btn_points.pack(fill=tk.X)
        self.widgets_to_disable.append(self.btn_points)
        self.lbl_points = ttk.Label(file_group, text="未选择文件", wraplength=300)
        self.lbl_points.pack()
        
        self.btn_matrix = ttk.Button(file_group, text="选择预估的点与点之间用时信息EXCEL文件", 
                 command=self.load_matrix_callback)
        self.btn_matrix.pack(fill=tk.X)
        self.widgets_to_disable.append(self.btn_matrix)
        self.lbl_matrix = ttk.Label(file_group, text="未选择文件", wraplength=300)
        self.lbl_matrix.pack()
        
        
        
        
    def disable_widgets(self):
        """Disable all interactive widgets in the panel"""
        for widget in self.widgets_to_disable:
            widget.configure(state='disabled')
            
    def enable_widgets(self):
        """Enable all interactive widgets in the panel"""
        for widget in self.widgets_to_disable:
            widget.configure(state='normal')
